<?php

$userid = get_loggedin_userid();
//$userid = get_input('user_id');
// Get variables
$friendid = get_input('friend_id');
$refererid = get_input('referer_id');
$client_id = get_input('client_id');

$response = get_input('response');

if(!$response) {
    $response = 'yes';
}

$result = check_whiteboard_friend($friendid, $userid);

if (!$result) {
    // This should not happen
} else {
    switch ($result['relationship']) {
        // Add a pending relationship, send notification
        case 'none':
            if($response == 'no') {
                break;
            }
        //    echo 'enter case none';
            request_whiteboard_friend($userid, $friendid);
            break;
        // Add a both relationship, remove 'to' (pending from friend to user) + remove all related notification
        case 'to':
       //     echo 'enter case to';
            if($response == 'yes') {
                accept_whiteboard_friend($friendid, $userid);
            } else {
                deny_whiteboard_friend($friendid, $userid);
            }
            break;
        // if response = no, Cancel friend request, remove pending relationship + notification, add a cancel notification
        case 'from':
        //	echo 'enter case from';
            if($response == 'yes') {
                break;
            }
            cancel_whiteboard_friend($userid, $friendid);
            break;
        // Remove friend if response = no
        case 'both':
            if($response == 'yes') {
                break;
            }
            remove_whiteboard_friend($userid, $friendid);
            break;
    };
}

$result = check_whiteboard_friend($friendid, $userid);

echo json_encode(export_result(200, $result));

?>